package com.woniuxy.springbootrabbit.controller;

import com.woniuxy.springbootrabbit.kill.KillProducter;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/kill")
public class KillController {
    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @Resource
    private KillProducter killProducter;

    @GetMapping("/start")
    public String start(String account){
        //1.判断库存是否足够
        int num = (Integer)redisTemplate.opsForValue().get("num");
        if (num > 0){
            //2.如果足够通过秒杀的生成对象将消息发送给rabbitmq，然后返回结果（当前请求结束了）
            killProducter.send(account,"kill");
            // 返回结果
            return "正在努力为您抢购中，请稍微等一会儿...";
        }

        //3.如果库存不够，返回结果，秒杀结束
        return "kill over";
    }
}
